<template>
  <el-dialog :visible.sync="visible" :title="title" width="500px" destroy-on-close append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="会员卡号" prop="memberNo">
          <el-input v-model="form.memberNo" placeholder="请输入会员卡号" />
      </el-form-item>
      <el-form-item label="姓名" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="初始金额" prop="memberInitAmount">
          <el-input v-model="form.memberInitAmount" placeholder="初始金额" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getMember, addMember, updateMember } from "@/api/system/member";
import mixin from '../mixin';
export default {
  mixins: [mixin],
  data() {
    return {
      // 表单校验
      rules: {
        memberName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        memberNo: [
          { required: true, message: "卡号不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        memberInitAmount: [
          { required: true, message: "初始金额不能为空", trigger: "blur" }
        ],
      }
    }
  },
  computed: {},
  mounted() {},
  methods: {
    initFormData (memberId) {
      getMember(memberId).then(response => {
        this.form = response.data;
      });
    },
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.memberId != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.visible = false;
              this.$emit("submit");
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.visible = false;
              this.$emit("submit");
            });
          }
        }
      });
    }
  }
}
</script>

